package com.bw.service.impl;

import com.bw.domain.*;
import com.bw.exception.MyRuntimeException;
import com.bw.mapper.OrderMapper;
import com.bw.mapper.WorkerMapper;
import com.bw.service.OrderService;
import com.bw.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author :Beyond🎸🎸
 * @date : 2025-03-08 11:03:14
 * @description Rock and Roll
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    WorkerMapper workerMapper;

    @Override
    public List<Order> list(Order order) {
        return orderMapper.list(order);
    }

    @Override
    public List<Community> listCommunity() {
        return orderMapper.listCommunity();
    }

    @Transactional(rollbackFor = MyRuntimeException.class)
    @Override
    public boolean fp(Order order, String select) {
        try {
//            更改工单状态,工单处理人员+分配时间
            orderMapper.update(order);
            //添加到工单明细
            orderMapper.addOrderDetail(order.getId(),select);
//            发送信息给工作人员
            String[] split = select.split(",");
            for (String name : split) {
                Worker worker = workerMapper.getByName(name);
            SmsUtil.sendCode(worker.getPhone(),"您有新的工单，请及时处理","3",1);
                System.out.println("短信已发给工作人员");
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw new MyRuntimeException("分配工单失败");
        }
    }

    @Override
    public List<Orderdetail> listDetailByOrderId(String orderId) {
        return orderMapper.listDetailByOrderId(orderId);
    }

    @Override
    public Order get(String orderId) {
        return orderMapper.get(orderId);
    }

    @Override
    public List<Orderdetail> getOrderDetail(String orderId) {
        return orderMapper.getOrderDetail(orderId);
    }

    @Override
    public int rate(Rate rate) {

      return  orderMapper.rate(rate);

    }

    @Override
    public List<Rate> ListRate(String orderId) {
        return orderMapper.ListRate(orderId);
    }

    @Override
    public List<Dict> listType(String type) {
        return orderMapper.listType(type);
    }

    @Override
    public int add(Order order) {
        return orderMapper.add(order);
    }

    @Override
    public List<Order> listByIds(String ids) {
        return orderMapper.listByIds(ids);
    }
}
